using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DTO;
using BUS;
using DAL;
using Utility;
using Encosia;

public partial class Admin_sanphamlist : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (Session["UserName"] == "")
        {
            Response.Redirect("dangnhap.aspx");
        }
        if (!IsPostBack)
        {
            LoadData();
        }
    }
    private void LoadData()
    {
        Criteria cri = new Criteria();
        if (txt_tensp.Text != "")
            cri.add(Condition.AND, tbsanphamcolumn.Tensp, Condition.LIKE, txt_tensp.Text);
        int total = 0;
        tbsanphamcollection sanphamcollection =
            tbsanphamBUS.GET(cri.Criter, 0, 1, tbsanphamcolumn.Masp.ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = sanphamcollection;
        Repeater1.DataBind();
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        //try
        //{
            if (e.Item.ItemType == ListItemType.AlternatingItem ||
            e.Item.ItemType == ListItemType.Item)
            {
                tbsanphamDTO sanpham = (tbsanphamDTO)e.Item.DataItem;
                HighslideImage hinh = (HighslideImage)e.Item.FindControl("img_hinh");
                tbhinhanhDTOColection hinhanh = tbhinhanhBUS.getid(sanpham.Masp);

               
                hinh.ImageUrl = "~/Admin/image/hinhnho/" + hinhanh[0].Tenhinhanh;
                hinh.FullImageURL = "~/Admin/image/thump/" + hinhanh[0].Tenhinhanh;
                //hinh.TemplateSourceDirectory = "~/Admin/image/thump/" + hinhanh[0].Tenhinhanh.ToString();

                HyperLink hyp_ten = (HyperLink)e.Item.FindControl("hyp_ten");
                HyperLink h = (HyperLink)e.Item.FindControl("hyp_Sua");
                Literal lit_ma = (Literal)e.Item.FindControl("lit_ma");

                HiddenField hf_id = (HiddenField)e.Item.FindControl("hf_id");
                hf_id.Value = sanpham.Masp.ToString();
                lit_ma.Text = sanpham.Masp.ToString();

                hyp_ten.NavigateUrl = "~/Admin/sanphamct.aspx?id=" + sanpham.Masp.ToString();
                hyp_ten.Text = sanpham.Tensp.ToString();

                HyperLink hyp_sua = (HyperLink)e.Item.FindControl("hpl_Edit");
                hyp_sua.NavigateUrl = "~/Admin/sanphamsua.aspx?id=" + sanpham.Masp.ToString();

                tbloaispDTO loasp = tbloaisanphamBUS.GetByID(sanpham.Maloaisp);
                Literal tenloai = (Literal)e.Item.FindControl("lt_tenloai");
                tenloai.Text = loasp.Tenloaisp.ToString();
                tbdoanhnghiepDTO doanhnghiep = tbdoanhnghiepBUS.GetByID(sanpham.Madn);
                HyperLink tendn = (HyperLink)e.Item.FindControl("hp_tendn");
                tendn.Text = doanhnghiep.Tendn.ToString();
                tendn.NavigateUrl = "~/Admin/doanhnghiepct.aspx?id=" + sanpham.Madn.ToString();

                tbtrangthaiDTO tt = tbtrangthaiBUS.GetByID(sanpham.Matrangthai);
                Literal trangthai = (Literal)e.Item.FindControl("lt_trangthai");
                trangthai.Text = tt.Tentrangthai.ToString();

                tbtinhthanhDTO tthanh = tbtinhthanhBUS.GetByID(sanpham.Matinhthanh);
                Literal t_thanh = (Literal)e.Item.FindControl("lt_tinhthanh");
                t_thanh.Text = tthanh.Tentinhthanh.ToString();
                HyperLink xemhinh = (HyperLink)e.Item.FindControl("lbt_xemhinh");
                xemhinh.NavigateUrl = "~/Admin/Danhsachhinh_theosp.aspx?id=" + sanpham.Masp.ToString();

            }

        //}
        //catch(Exception y)
        //{
        //    Response.Redirect(y.ToString());
        //}
        
    }
    protected void btn_Timkiem_Click(object sender, EventArgs e)
    {
        LoadData();
    }
    protected void btn_xoa_Click(object sender, EventArgs e)
    {
       
       foreach (RepeaterItem item in this.Repeater1.Items)
        {
            CheckBox cb = (CheckBox)item.FindControl("cb_xoa");
            if (cb.Checked == true)
            {
                HiddenField hf_id = (HiddenField)item.FindControl("hf_id");
                tbhinhanhDTOColection ha = tbhinhanhBUS.getid(int.Parse(hf_id.Value));                  
                if (ha.Count>=0)
                {
                    for (int i=0; i < ha.Count;i++ )
                    {
                            tbhinhanhBUS.Delete(ha[i].Mahinhanh);                        
                        
                    }
                    
                }
                tbsanphamBUS.Delete(int.Parse(hf_id.Value));
                
            }
        }
        LoadData();
    }
    protected void btn_them_Click(object sender, EventArgs e)
    {
        Response.Redirect("sanphamthem.aspx");
    }
    protected void bt_loaisanpham_Click(object sender, EventArgs e)
    {
        Response.Redirect("loaisanphamlist.aspx");
    }
}
